<template>
  <aside class="sidebar" :class="{ 'sidebar-collapsed': isCollapsed }">
    <div class="sidebar-header">
      <h3 v-if="!isCollapsed">{{ title }}</h3>
      <button class="toggle-btn" @click="toggleSidebar">
        {{ isCollapsed ? '>' : '<' }}
      </button>
    </div>
    
    <nav class="sidebar-nav">
      <ul>
        <li v-for="item in menuItems" :key="item.path">
          <a
            :class="['nav-link', {active: item.path === activePath }]"
            :title="item.title"
            @click="toggleSide(item)"
          >
            <i v-if="item.icon" :class="item.icon"></i>
            <span>{{ item.title }}</span>
          </a>
        </li>
      </ul>
    </nav>
  </aside>
</template>

<script setup>
import { ref } from 'vue';

const props = defineProps({
  title: {
    type: String,
    default: '导航菜单'
  },
  menuItems: {
    type: Array,
    default: () => []
  }
});
const emits = defineEmits(['defineEmits']);
const isCollapsed = ref(false);
const activePath = ref('/introduce')
const toggleSidebar = () => {
  emits('toggle', isCollapsed.value);
  isCollapsed.value = !isCollapsed.value;
};
const toggleSide = (item) => {
  activePath.value = item.path;
  emits('toggle-side', item);
}
</script>

<style scoped>
.sidebar {
  width: 250px;
  height: 100vh;
  background-color: #2c3e50;
  color: white;
  transition: all 0.3s ease;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1000;
  overflow-y: auto;
}

.sidebar-collapsed {
  max-width: 100px;
}

.sidebar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.toggle-btn {
  background: none;
  border: none;
  color: white;
  cursor: pointer;
  font-size: 1.2rem;
  padding: 5px;
}

.sidebar-nav ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

.sidebar-nav li {
  margin: 5px 0;
}

.nav-link {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 12px 0;
  color: white;
  text-decoration: none;
  transition: background-color 0.3s;
}

.nav-link:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.nav-link i {
  margin-right: 10px;
  width: 20px;
  text-align: center;
}

.sidebar-collapsed .nav-link span {
  /* display: none; */
}

.sidebar-collapsed .nav-link i {
  margin-right: 0;
  font-size: 1.2rem;
}

.active {
  background-color: #42b983;
  border-left: 3px solid white;
}
</style>
